<?php
require_once (INCLUDEPATH.'/simplehtmldom/simple_html_dom.php');

function update_wp_creative(){
	global $wpdb; 
	$rows = $wpdb->get_results('SELECT link from wp_creative');
	foreach ($rows as $row){
		$path = array_reverse(explode('/',$row->link));
		$group = $path[0];
		$wpdb->update('wp_creative',array('group' => $group),array('link' => $row->link));
	}
}

//get_birthday();
function get_birthday(){
	global $wpdb;
	set_time_limit(0); // set no time limit to download large file
	ini_set('user_agent', 'Mozilla');
	ini_set('display_errors',true);//Just in case we get some errors, let us know....
	
	$url= "http://www.trendsnow.net/book-creatifs";
	$list = file_get_html($url);
	$count = 0;
	
	// Parse detail pages (url defined in sidebar menu)
	foreach($list->find('.bookc_sidebar-list li a') as $element){
	
		$html = file_get_html($element->href);

		//BIRTHDAY_DATE
		$birthday_date = '';
		$birthday_isset = false;
		$origin_isset = false;
		foreach($html->find('#bookc-box-infos-civil h4') as $element_cat){
			if (trim($element_cat->plaintext) == 'Origine :') $origin_isset = true; 
			if (trim($element_cat->plaintext) == 'Date de Naissance :') $birthday_isset = true; 
		}
		if ($birthday_isset) {
			$civil_info = $html->find('#bookc-box-infos-civil',0)->innertext;
			//echo $civil_info;
			$pattern = '/<\/h4[^>]*>(.*?)<br\/>/';
			$offset = 0;
			if($origin_isset){
				preg_match($pattern, $civil_info, $matches,PREG_OFFSET_CAPTURE,18);
				$offset = $matches[1][1];
			}
			preg_match($pattern, $civil_info, $matches,PREG_OFFSET_CAPTURE,$offset);
			$birthday_date = $matches[1][0];
			//echo 'Date de Naissance :' . $birthday_date . '<br>';
			$wpdb->update('wp_creative',array('birthday_date' => $birthday_date),array('link' => $element->href));
		}
		//if ($count > 3) exit();
		$count++;
	}
}

// get_thumbnails();
function get_thumbnails(){

	$url= "http://www.trendsnow.net/book-creatifs";
	ini_set('user_agent', 'Mozilla');
	$list = file_get_html($url);

	foreach($list->find('.bookc_sidebar-list li a') as $element){
		
		//Load page detail into $html and thumbnail related into photo_url
		$html = file_get_html($element->href);
		$photo_url = $html->find('#bookc-box-infos-thumb img',0)->src;
		
		//Define photo link
		$photo_pattern = '/src=(.*?\.jpg)[^jpg]/'; 
		preg_match($photo_pattern, $photo_url, $matches);
		$photo_path = $matches[1];
		//$photo_url = "http://www.trendsnow.net/cms/wp-content/themes/tn2010/scripts/timthumb.php?src=http://www.trendsnow.net/cms/uploads/2011/01/sergio_villaquiran.jpg&w=150&h=150&zc=1&q=70";
		//$photo_path = "http://www.trendsnow.net/cms/uploads/2011/01/jonathan_andrew.jpg";
		echo $photo_path. '<br>';


		//Define image name
		$elements_path = array_reverse(explode('/',$element->href));
		$image_name = $elements_path[0];
		//$element->href= "http://www.trendsnow.net/book-creatifs/jonathan-andrew";
		//$image_name = jonathan-andrew;
		echo $image_name. '<br>';
		
		//Auto-download image into $dir file
		$dir = TEMPLATEPATH . "/files/$image_name.jpg"; 
		$fp = fopen ($dir, 'w+');//where the file will be saved
		$ch = curl_init($photo_path);//Here is the file we are downloading
		curl_setopt($ch, CURLOPT_TIMEOUT, 50);
		curl_setopt($ch, CURLOPT_FILE, $fp);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_exec($ch);
		curl_close($ch);
		fclose($fp);
	}
}

// get_details();
function get_details(){
	global $wpdb;
	set_time_limit(0); // set no time limit to download large file
	ini_set('user_agent', 'Mozilla');
	ini_set('display_errors',true);//Just in case we get some errors, let us know....
	
	$url= "http://www.trendsnow.net/book-creatifs";
	$list = file_get_html($url);
	
	// Parse detail pages (url defined in sidebar menu)
	$count = 0;
	foreach($list->find('.bookc_sidebar-list li a') as $element){
	
		$html = file_get_html($element->href);
	
		//NAME
		$name = $element->title;
		echo $name . '<br>';
		
		//LINK
		$link = $element->href;
		echo $link . '<br>';
		
		//PHOTO URL
		$photo_url = '';
		$photo_url = $html->find('#bookc-box-infos-thumb img',0)->src;
		echo $photo_url . '<br>';
		
		//ORIGIN
		$orogin = '';
		$title1 = $html->find('#bookc-box-infos-civil h4',0)->plaintext;
		if ($title1 == 'Origine :')	
			$origin = $html->find('#bookc-box-infos-civil a[rel=tag]',0)->innertext;
		echo $origin . '<br>';
		
		//BIRTHDAY_DATE
		$birthday_date = '';
		$birthday_isset = false;
		$origin_isset = false;
		foreach($html->find('#bookc-box-infos-civil h4') as $element_cat){
			if (trim($element_cat->plaintext) == 'Origine :') $origin_isset = true; 
			if (trim($element_cat->plaintext) == 'Date de Naissance :') $birthday_isset = true; 
		}
		if ($birthday_isset) {
			$civil_info = $html->find('#bookc-box-infos-civil',0)->innertext;
			//echo $civil_info;
			$pattern = '/<\/h4[^>]*>(.*?)<br\/>/';
			$offset = 0;
			if($origin_isset){
				preg_match($pattern, $civil_info, $matches,PREG_OFFSET_CAPTURE,18);
				$offset = $matches[1][1];
			}
			preg_match($pattern, $civil_info, $matches,PREG_OFFSET_CAPTURE,$offset);
			$birthday_date = $matches[1][0];
			//echo 'Date de Naissance :' . $birthday_date . '<br>';
			$wpdb->update('wp_creative',array('birthday_date' => $birthday_date),array('link' => $element->href));
		}
		
		//DOMAIN
		$domain ='';
		$count = 0;
		foreach($html->find('#bookc-box-infos-civil a[rel=tag]') as $element_domain){
			if ($origin == '') 
				$count++;
			if ($count > 0) 
				$domain = $domain . ',' . $element_domain->innertext; 
			$count++; 
		}
		$domain = ltrim($domain,',');
		echo $domain . '<br>';
		
		//WEBSITE
		$website = '';
		$website = $html->find('#bookc-box-infos-civil a',0)->href;
		echo $website . '<br>';
		
		//BIBLIOGRAPHY
		$biography ='';
		$count = 0;
		foreach($html->find('#bookc-box-bio-2 p') as $element_biography){
			if ($count > 0) 
				$biography = $biography . $element_biography->outertext; 
			$count++; 
		}
		echo $biography . '<br><hr>';
		
		
		
		$wpdb->insert('wp_creative', array( 
		 	'name' => $name, 
			'photo_url' => $photo_url,
		 	'origin' => $origin, 
			'birthday_date' => $birthday_date,
		 	'domain' => $domain, 
			'website' => $website,
		 	'biography' => $biography, 
			'link' => $link
		));
		$wpdb->show_errors();
	}
}	